/*
 * @Author: cola(sunshiwen)
 * @Date: 2022-04-22 10:05:24
 * @LastEditTime: 2022-09-29 17:37:28
 * @LastEditors: liuzhou
 * @Description: 分支管理 controller
 * @FilePath: /maycur-koa/controller/staticFilesManageModel.js
 */
const {
    staticFiles,
} = require('../model/staticFilesManageModel');
const Sequelize = require('sequelize');
const Op = Sequelize.Op;

module.exports = {
    fetchStaticFilesList: async ctx => {
        const {
            length,
            start,
            keyword
        } = ctx.reqParams.body;
        let data = await staticFiles.findAndCountAll({
            order:[['updatedAt', 'DESC']],
            limit: length,
            offset: start,
            where: { invalid: 0, 
                [Op.or]:[
                    { name: { [Op.like]: `%${keyword}%` } },
                    { fileLink: { [Op.like]: `%${keyword}%` } },
                ]
            } 
        });
        
        ctx.body = data;
    },

    addStaticFiles: async ctx => {
        const {
            name,
            remarks,
            fileLink,
            directory,
            fileList
        } = ctx.reqParams.body;
        
        await staticFiles.create({
            name,
            remarks,
            fileLink,
            directory,
            fileList
        });

        ctx.body = 'SUCCESS';
    },

    updateStaticFiles: async ctx => {
        const {
            name,
            remarks,
            id,
            fileList
        } = ctx.reqParams.body;
        
        await staticFiles.update({
            name,
            remarks,
            fileList
        }, {
            where: {
                id: id
            }
        });

        ctx.body = 'SUCCESS';
    },

    deleteStaticFile: async ctx => {
        const { id } = ctx.reqParams.router;
        const data = await staticFiles.update({invalid: 1}, {
            where: {
                id,
            }
        });
        if (data <= 0) {
            throw new Error('删除失败');
        }
        ctx.body = 'SUCCESS';
    }
};